<?php


namespace App\Http\Controllers\Admin\Exchange;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Customers;
use App\Models\Exchanges;
use App\Models\Shops;
use App\Models\Goods;
use App\Models\Users;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

/**
 * 兑换列表控制器
 *
 * @author yh 20200329
 * @package App\Http\Controllers\Shops
 */
class ExchangesController extends BaseAdminController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 兑换列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Exchanges::orderBy($sort, $order);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } elseif (is_mobile(request('title'))) {
                $query->where('phone', request('title'));
            } else {
                $query->where('username', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
        // 取得列表
        $shops = $query->select([
            'id',
            'score',
            'size',
            'title',
            'des',
            'author',
            'phone',
            'status',
            'exchange_nums',
            'money',
            'type',
            'created_at'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $shops->appends(app('request')->all());

        return view('admin.exchange.exchanges', [
            'shops' => $shops,
        ]);
    }
    /**
     * 添加积分兑换商品
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170825
     */
    public function shops()
    {
        $shops = Shops::where('status', 1)->get();
        $goods = Goods::where('status', 1)->where('shop_id',request('shop_id'))->get();
       return  response_json(1,$goods,'success');
    }

    /**
     * 添加积分兑换商品
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170825
     */
    public function add()
    {
        $shops = Shops::where('status', 1)->get();
        return view('admin.exchange.exchanges_form', ['shops' => $shops]);
    }

    /**
     * 编辑用户
     *
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170111
     */
    public function edit($id)
    {
        // 取得用户信息
        $user = Exchanges::findOrFail($id);

        $shops = Shops::where('status', 1)->get();
        $goods = Goods::where('status', 1)->where('shop_id',$user->shop_id)->get();
        $customers = Customers::all();

        return view('admin.exchange.exchanges_form', [
            'user' => $user,
            'shops' => $shops,
            'goods'=>$goods,
            'customers' => $customers
        ]);
    }


    /**
     * 商品兑换选择
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170206
     */
    public function save()
    {

        // 保存用户信息
        DB::transaction(function () {
            $user = Exchanges::findOrNew(request('id'));
            $type = request('type');
            $money = request('money');
            $author_id = 0;
            $nickname = "";
            $size = 0;
            $title = 0;
            $shop_id = 0;
            $avatar = "";
            $phone = "";
            $cate_id = 0;
            $img = '';
            if ($type == 2) {
                $money = request('money');
                $img = request('img');
                $author_id = 3;
                $user->cate_id = $cate_id;
                $user->endDate = request('endDate');
                $user->exchange_nums = request('exchange_nums');
                $user->money = $money;
                $user->nickname = $nickname;
                $user->author_id = $author_id;
                $user->size = $size;
                $user->title = $title;
                $user->img = $img;
                $user->shop_id = $shop_id;
                $user->good_id = request('good_id', 0);
                $user->avatar = $avatar;
                $user->phone = $phone;
                $user->type = $type;
                $user->rent_nums = request('exchange_nums');
                $user->score = request('score');
                $user->status = 1;
                $user->save();
            }
            if ($type == '1') {
                $shops = Goods::where('id', request('good_id'))->first();
                $shops->is_exchange = 1;
                $shops->score = request('score');
                $shops->rent_nums = request('exchange_nums');
                $shops->total = request('exchange_nums');
                $shops->save();
                $size_num_arr = request('size_num');
                $size_name_arr = request('size_name');
                $total = 0;

                $exhibit_num_json = [];
                if(isset($size_num_arr)){
                    foreach ($size_num_arr as $k => $g) {
                        $exhibit_num_json[] = [
                            'size_name' => $size_name_arr[$k],
                            'size_num' => $g,

                        ];
                        $total = $total + $g;
                    }
                    $user['size'] = json_encode($exhibit_num_json);
                }else{

                    $user['size'] = '';
                }


                $s = Shops::where('id', $shops['shop_id'])->first();

                $author_id = $s['uid'];
                $uinfo = Users::where('uid', $s['uid'])->first();
                $nickname = $uinfo->nickname;
                $size = $shops->size;
                $title = $shops->title;
                $cate_id = $shops->cate_id;
                $shop_id = $shops->shop_id;
                $avatar = $shops->avatar;
                $phone = $shops->phone;
                $exchange_nums = request('exchange_nums');
//                if ($exchange_nums > $shops->total) {
//                    return $this->validate(request(), [], [], [
//                        'exchange_nums' => '兑换数量不能超过上面数量',
//                    ]);
//                }
                $user->cate_id = $cate_id;
                $user->endDate = request('endDate');
                $user->exchange_nums = request('exchange_nums');
                $user->rent_nums = request('exchange_nums');
                $user->money = $money;
                $user->nickname = $nickname;
                $user->author_id = $author_id;

                $user->title = $title;
                $user->img = $img;
                $user->shop_id = $shop_id;
                $user->good_id = request('good_id', 0);
                $user->avatar = $avatar;
                $user->phone = $phone;
                $user->type = $type;
                $user->score = request('score');
                $user->status = 1;
                $user->save();
            }
        });

        return $this->success(get_session_url('index'));
    }

    /**
     * 删除用户
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Exchanges::destroy($id);
            return $this->success();
        }
    }

    /**
     * 审核店铺
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author yh 20200322
     */
    public function refund()
    {
        $user = Exchanges::findOrNew(request('id'));
        $user->status = intval(request('status'));
        $rule = [];
        if (request('status') == 0) {
            $rule = array_merge([
                'refund_msg' => 'required'
            ], $rule);
            $this->validate(request(), $rule, [], [
                'refund_msg' => '审核不通过原因',
            ]);
        }
        $user->refund_msg = request('refund_msg');
        $user->save();
        return $this->success(get_session_url('index'));
    }
}